package com.cbs.android.widget.fullhtmlview;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.cbs.android.widget.nospacetextview.NoSpaceTextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * TextViewForFullHtml是一个增强的TextView,修改了TextView.fromHtml不能设置
 * 字体大小的问题(原生的只能设置small\normal\big),同时,兼容了ActionScript脚本
 */
public class FullHtmlView extends NoSpaceTextView {
    private static LinkStyle defaultLinkStyle = new DefaultLinkStyle();
    private static int defaultHighlightColor = Color.TRANSPARENT;
    private LinkStyle linkStyle = null;
    private boolean hrefClicked = false;
    private OnClickListener onClickListener = null;

    public static void setDefaultLinkStyle(LinkStyle defaultLinkStyle) {
        FullHtmlView.defaultLinkStyle = defaultLinkStyle;
    }

    public static void setDefaultHighlightColor(int defaultHighlightColor) {
        FullHtmlView.defaultHighlightColor = defaultHighlightColor;
    }

    public void setLinkStyle(LinkStyle linkStyle) {
        this.linkStyle = linkStyle;
    }

    public FullHtmlView(Context context) {
        super(context);
        init();
    }

    public FullHtmlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FullHtmlView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        setHighlightColor(defaultHighlightColor);
    }

    private void txtViewSetText(TextView view, CharSequence text, BufferType type) {
        CharSequence t = text;
        if (t instanceof Spannable) {
            int end = text.length();
            Spannable sp = (Spannable) t;
            URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
            SpannableStringBuilder style = new SpannableStringBuilder(text);
            if (urls.length > 0) {
                view.setMovementMethod(LinkMovementMethod.getInstance());
                for (URLSpan url : urls) {
                    style.removeSpan(url);
                    HJURLSpan myURLSpan = new HJURLSpan(url.getURL(), onClickListener);
                    style.setSpan(myURLSpan, sp.getSpanStart(url),
                            sp.getSpanEnd(url),
                            Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                }
            }
            view.setText(style, type);
        }
    }

    public void loadContent(String content) {
        int gravity = Gravity.NO_GRAVITY;
        content = content.replace("<P", "<d");
        content = content.replace("</P>", "</d><br/>");

        content = ActionscriptTextUtils.parseFontHTML(content);

        String p_regex = "<d ALIGN=\"(LEFT|CENTER|RIGHT)\">";
        Pattern p2 = Pattern.compile(p_regex);
        Matcher m2 = p2.matcher(content);
        String c_str = "left";
        while (m2.find()) {
            c_str = m2.group(1);
            break;
        }
        if (c_str.equalsIgnoreCase("LEFT")) {
            gravity = Gravity.LEFT;
        } else if (c_str.equalsIgnoreCase("CENTER")) {
            gravity = Gravity.CENTER;
        } else if (c_str.equalsIgnoreCase("RIGHT")) {
            gravity = Gravity.RIGHT;
        }
        Spanned spaned = FullHtml.fromHtml(getContext(), content);
        txtViewSetText(FullHtmlView.this, spaned, BufferType.SPANNABLE);
        setGravity(gravity);
    }

    public void setOnClickListener(OnClickListener listener) {
        this.onClickListener = listener;
        super.setOnClickListener(new MyOnClickListener(listener));
    }

    public interface OnClickListener {
        void onClick(View v);

        void onHref(View v, String href);
    }

    private class HJURLSpan extends URLSpan {
        private String mUrl;
        private OnClickListener onClickListener = null;

        public HJURLSpan(String url, OnClickListener onClickListener) {
            super(url);
            mUrl = url;
            this.onClickListener = onClickListener;
        }

        @Override
        public void onClick(View widget) {
            if (this.onClickListener != null) {
                onClickListener.onHref(widget, mUrl);
                hrefClicked = true;
            }
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            if (linkStyle != null) {
                linkStyle.updateDrawState(ds);
            } else {
                defaultLinkStyle.updateDrawState(ds);
            }
        }
    }

    private class MyOnClickListener implements View.OnClickListener {
        private OnClickListener onClickListener = null;

        public MyOnClickListener(OnClickListener onClickListener) {
            this.onClickListener = onClickListener;
        }

        @Override
        public void onClick(View v) {
            if (this.onClickListener != null) {
                if (hrefClicked) {
                    hrefClicked = false;
                } else {
                    this.onClickListener.onClick(v);
                }
            }
        }
    }
}
